
file(GLOB_RECURSE _files
    *.h
    *.c
    *.cpp
    *.hlsl
)

file(GLOB_RECURSE _shaders
    *.hlsl
)

# execute_process(
#     COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_SOURCE_DIR}/CompiledShaders
# )

foreach(_hlsl_file ${_shaders})
    get_filename_component(_file_name ${_hlsl_file} NAME_WE)        
    string(REGEX MATCH "[A-Z][a-z]*" _shader_name ${_file_name})
    string(REGEX MATCH "[A-Z][a-z]*Shader" _shader_type ${_file_name})
    # message(${filename} "   " ${_shader_name} "   " ${_shader_type})
    if(_shader_type STREQUAL VertexShader)
        execute_process(
            COMMAND ${DXC_PATH}/dxc.exe -Zpc -E main -T vs_6_0 -Vn g_${_file_name} -Fh ${CMAKE_CURRENT_SOURCE_DIR}/${_file_name}.hlsl.h ${_hlsl_file}
        )
    elseif(_shader_type STREQUAL PixelShader)
        execute_process(
            COMMAND ${DXC_PATH}/dxc.exe -Zpc -E main${_shader_name} -T ps_6_0 -Vn g_${_file_name} -Fh ${CMAKE_CURRENT_SOURCE_DIR}/${_file_name}.hlsl.h ${_hlsl_file}
        )
    else()
        message(FATAL_ERROR "Shader type not yet supported!")
    endif()
    # message(${_file_name} " -> " ${_file_name}.hlsl.h)

endforeach()
    

file(GLOB_RECURSE _assets
    *.bin
)

# list(FILTER sources EXCLUDE REGEX "^${CMAKE_CURRENT_SOURCE_DIR}/CMakeFiles")

# target_include_directories(selahengine PRIVATE ./)
# target_include_directories(selahengine PRIVATE ./External/imgui/)

# /*$(if:DX12.AgilitySDKRequired:true)*/target_include_directories(selahengine PRIVATE ./AgilitySDK/include/)/*$(endif)*/

set(_exec PipelineCache)
set(WIN_PIX ON)
# add_executable(${_exec})
# target_sources(${_exec} PUBLIC ${_files})

add_executable(${_exec} WIN32 ${_files})

target_precompile_headers(${_exec} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/stdafx.h)

target_compile_features(${_exec} PRIVATE cxx_std_17)
target_compile_definitions(${_exec} PRIVATE "UNICODE" "_UNICODE")
target_link_libraries(${_exec} d3d12.lib dxgi.lib d3dcompiler.lib dxguid.lib)

target_include_directories(${_exec} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/util)
target_include_directories(${_exec} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

if(WIN_PIX)
    target_compile_definitions(${_exec} PRIVATE USE_PIX)

    target_link_libraries(${_exec} Pix)
    add_custom_command(
        TARGET ${_exec}
        POST_BUILD
        COMMAND "${CMAKE_COMMAND}" -E copy ${PIX_LIB_DIR}/WinPixEventRuntime.dll $<TARGET_FILE_DIR:${_exec}>
    )
endif()


source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${_files})

# set_source_files_properties(${_shaders} PROPERTIES HEADER_FILE_ONLY TRUE)

# set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT selahengine)

add_custom_command(
    TARGET ${_exec}
    POST_BUILD
    COMMAND "${CMAKE_COMMAND}" -E copy ${_shaders} $<TARGET_FILE_DIR:${_exec}>
)


if(NOT ${_assets})
    add_custom_command(
        TARGET ${_exec}
        POST_BUILD
        COMMAND "${CMAKE_COMMAND}" -E copy ${_assets} $<TARGET_FILE_DIR:${_exec}>
    )
endif()
